package com.lims.algorithm.useful.stringprocess;

/**
 * <功能简述>中文文本匹配率
 *
 * @author: lims
 * @create: 2019-08-06 17:11
 */
public class ChineseStringMatcher {

    /**
     *
     */
    /*public static final char MATCH = '十';
    public static final char MISMATCH = '口';*/
    public char MATCH = '十';
    public char MISMATCH = '口';

    /**
     *
     */
    private static final char[] cnSignArr = "。？！，、；：“” ‘’「」『』（）〔〕【】—﹏…～·《》〈〉".toCharArray();
    private static final char[] enSignArr = "`!@#$%^&*()_+~-=[]\\;',./{}|:\"<>?".toCharArray();

    /**
     * orgWords:原串
     * mtcWords：目标串
     */
    private String originString;
    private String matchString;

    private String orgWords;
    private String mtcWords;

    /**
     *
     */
    private char[] org;
    private char[] mtc;

    /**
     * 指示器
     */
    private int[] indicator;

    /**
     *
     */
    private double matchingRate;
    private double accuracyRate;
    private int absent;
    private int excess;


    public ChineseStringMatcher(String orignText, String matchText) {
        super();

        this.originString = orignText;
        this.matchString = matchText;

        //init variables
        initVariables();

        //do process
        doProcess();
    }


    private void initVariables() {
        //init char array

        orgWords = clearNonWordChar(originString);
        mtcWords = clearNonWordChar(matchString);

        org = orgWords.toCharArray();
        mtc = mtcWords.toCharArray();

        //init indicator
        int i = 0;
        // init with 0
        indicator = new int[mtc.length];

    }


    public int[] getIndicator() {
        return indicator;
    }


    public double getMatchingRate() {
        return matchingRate;
    }

    public double getAccuracyRate() {
        return accuracyRate;
    }


    public int getAbsent() {
        return absent;
    }


    public int getExcess() {
        return excess;
    }

    public String getOriginString() {
        return originString;
    }


    public String getMatchString() {
        return matchString;
    }


    public char[] getOrg() {
        return org;
    }


    public char[] getMtc() {
        return mtc;
    }

    private String clearNonWordChar(String str) {
        for (int i = 0; i < cnSignArr.length; i++)
            str = str.replace("" + cnSignArr[i], "");
        for (int i = 0; i < enSignArr.length; i++)
            str = str.replace("" + enSignArr[i], "");
        return str;
    }

    public void doProcess() {
        int index = 0;
        double matchCount = 0;
        for (int i = 0; i < mtc.length; i++) {
            for (int j = index; j < org.length; j++) {
                if (mtc[i] == org[j]) {
                    index++;
                    indicator[i] = 1;
                    matchCount++;
                    break;
                }
            }
        }

        //匹配率
        matchingRate = matchCount / orgWords.length() * 100;
        //准确率
        accuracyRate = matchingRate * matchCount / mtcWords.length();

        if (orgWords.length() > matchCount) {
            absent = orgWords.length() - (int) matchCount;
        }
        if (mtcWords.length() - (int) matchCount != 0) {
            excess = mtcWords.length() - (int) matchCount;
        }
    }

    public void printResult() {
        System.out.println(" - - - - Start - - - - ");
        System.out.printf("%-8s%8s\n", "原串：", originString);
        System.out.printf("%-8s%8s\n", "目标串：", matchString);

        System.out.println();
        System.out.printf("%-16s\n", "匹配结果："
                + "");
        for (int i = 0; i < mtc.length; i++) {
            System.out.printf("%2c", mtc[i]);
        }
        System.out.println();
        for (int i : indicator) {
            //System.out.printf("%2c", indicator[i] == 0 ? MATCH : MISMATCH);
            System.out.printf("%2c", i == 1 ? MATCH : MISMATCH);
        }
        System.out.println();

        System.out.printf("匹配率：%.2f%%\n", matchingRate);
        System.out.printf("准确率：%.2f%%\n", accuracyRate);
        System.out.println();
        if (absent != 0) {
            System.out.printf("\n匹配到的字比原串少%d个字\n", absent);
        }
        if (excess != 0) {
            System.out.printf("\n匹配到的字比目标串少%d个字\n", excess);
        }
        System.out.println(" - - - - End - - - - ");
    }

    public static void main(String[] arg) {
        System.out.println("hello world!");
        ChineseStringMatcher matcher =
                new ChineseStringMatcher("我们爱国", "我热爱祖国");
        matcher.printResult();

        ChineseStringMatcher matcher2 =
                new ChineseStringMatcher("博客园科技发展北京有限公司", "博客园科技发展有限公司");
        matcher2.printResult();

        new ChineseStringMatcher("今天是星期五", "今天星期四").printResult();
        new ChineseStringMatcher("博客园科技发展（北京）有限公司", "博客园科技发展有限公司").printResult();
        new ChineseStringMatcher("我热爱祖国，热爱计算机科学", "我热爱计算机科学").printResult();

        new ChineseStringMatcher("南京大区", "南京大区").printResult();
        new ChineseStringMatcher("南京大区", "南京大区三八店").printResult();
        new ChineseStringMatcher("南京大区", "苏宁小店南京大区迈皋桥店").printResult();
    }
}
